window.onload = function() {
	//document.getElementById("register").onclick = validate;
	document.getElementsByName("tema")[0].onchange = changeTheme;
	document.getElementsByName("username")[0].onkeyup = validate;
	document.getElementsByName("password")[0].onkeyup = validate;
	document.getElementsByName("confirm_password")[0].onkeyup = validate;
	document.getElementsByName("real_name")[0].onkeyup = validate;
	document.getElementsByName("birthday")[0].onkeyup = validate;
	document.getElementsByName("email")[0].onkeyup = validate;
	document.getElementsByName("avatar")[0].onchange = validate;
	document.getElementsByName("sex")[0].onchange = validate;
};

function changeTheme() {
	var tema = document.getElementsByName("tema")[0];
	if(tema.options[tema.selectedIndex].value == "klasik") {
		var cssfile = document.getElementById("cssfile");
		cssfile.setAttribute("href","style1.css");
	} else if(tema.options[tema.selectedIndex].value == "pelangi"){
		var cssfile = document.getElementById("cssfile");
		cssfile.setAttribute("href","style2.css");
	}
}

function validate() {
	var res = validateForm();
	if(res == 0) {
		document.getElementsByName("status")[0].value = res;
		//document.getElementsByName("status")[0].value = document.getElementsByName("username")[0].value;
		document.getElementById("register").disabled = false;
		//window.open("http://www.google.com","_self",false);
	} else {
		document.getElementsByName("status")[0].value = res;
		//document.getElementsByName("status")[0].value = document.getElementsByName("username")[0].value;
		if(res != 0) document.getElementById("register").disabled = true;
	}
}

// return value:
// 0: form valid
// 1: username < 5 karakter
// 2: password < 8 karakter
// 3: username == pasword
// 4: password == email
// 5: konfirmasi password salah
// 6: nama tidak valid (minimal mengandung spasi diantara 2 karakter)
// 7: birthday tidak valid (YYYY-MM-DD)
// 8: email tidak valid
// 9: avatar tidak valid (.jpeg atau .jpg saja)
//10: gender belum dipilih
function validateForm() {
	var uname = document.getElementsByName("username")[0].value;
	var pass = document.getElementsByName("password")[0].value;
	var cpass = document.getElementsByName("confirm_password")[0].value;
	var rname = document.getElementsByName("real_name")[0].value;
	var bday = document.getElementsByName("birthday")[0].value;
	var email = document.getElementsByName("email")[0].value;
	var ava = document.getElementsByName("avatar")[0].value;
	var sex = document.getElementsByName("sex")[0];
	
	if(uname.length < 5) return 1;
	if(pass.length < 8) return 2;
	
	if(uname == pass) return 3;
	if(pass == email) return 4;
	
	if(cpass != pass) return 5;
	
	var re = /\S+ \S+/;
	if(!re.test(rname)) return 6;
	
	var re = /\d{4}-\d{2}-\d{2}/;
	if(!re.test(bday)) return 7;
	
	var re = /\S+@\S+\.\S+\S+/;
	if(!re.test(email)) return 8;
	
	var re = /\S+.jpeg|\S+.jpg/;
	if(!re.test(ava)) return 9;
	
	if(sex.options[sex.selectedIndex].value == "select_gender") return 10;
	
	return 0;
}
